package org.apache.lucene.demo;

import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

public class AnalyzerUtils {
	
	private static Analyzer analyzer = null;
	
	public static Analyzer getInstance(){
		if(analyzer == null){
			synchronized (AnalyzerUtils.class) {
				if(analyzer==null){
					analyzer = new WhitespaceAnalyzer();//避免多次多次初始化分词器，加载字典耗时长
				}
			}
		}
		return analyzer;
	}
	public static List<String> tokenStream(Analyzer analyzer, Reader analyzerReader) throws IOException {
		List<String> result = new ArrayList<String>();
		TokenStream tokenStream = analyzer.tokenStream(null, analyzerReader);
		CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class);
		while (tokenStream.incrementToken()) {
			result.add(attribute.toString());
		}
		return result;
	}
	
}
